<?php

setlocale(LC_ALL, "ru_RU.UTF-8");
mb_internal_encoding("UTF-8");

$curRealDir = mb_substr($_SERVER["SCRIPT_FILENAME"], 0, mb_strrpos($_SERVER["SCRIPT_FILENAME"], "/"));

require_once ("./config/setup.php");
require_once ("./libs/db.class.php");
require_once ("./libs/utils.inc.php");
require_once ("./libs/pclzip.lib.php");
$sm = new SSmarty();
$db = new DB();

$db->dbq("
            Select
                newid as newid,
                caption as caption,
                maintext as maintext,
                DATE_FORMAT(postdate, \"%d %M %Y, %H:%i\") as postdate,
                author as author,
                archiveid as archiveid,
                tags as tags,
                position as position,
                (
                    Select
                        Count(*)
                    From
                        comments
                    Where
                        newid = news.newid
                ) as countComments
            From
                news
            Order By
                position DESC
        ");

$news = array();
$previewImages = array();
while ($arr = $db->assoc()) {
    $matches = array();
    preg_match("'\[img=\"?([^\]]*)\"?\][\s]*(.*)[\s]*\[/img\]'iu", $arr["maintext"], $matches);

    $arr["image"] = array();

    if ($matches[1] == "preview") {
        $arr["image"]["url"] = mb_substr($matches[2], 1);

        if ($matches[1] != "") {
            $type = exif_imagetype($curRealDir . $matches[2]);
            switch ($type) {
                case 1:
                    $image = imagecreatefromgif($curRealDir . $matches[2]);
                    $arr["image"]["width"] = imagesx($image);
                    $arr["image"]["height"] = imagesy($image);
                    imagedestroy($image);
                    break;
                case 2:
                    $image = imagecreatefromjpeg($curRealDir . $matches[2]);
                    $arr["image"]["width"] = imagesx($image);
                    $arr["image"]["height"] = imagesy($image);
                    imagedestroy($image);
                    break;
                case 3:
                    $image = imagecreatefrompng($curRealDir . $matches[2]);
                    $arr["image"]["width"] = imagesx($image);
                    $arr["image"]["height"] = imagesy($image);
                    imagedestroy($image);
                    break;
                default:
                    $arr["image"]["width"] = 0;
                    $arr["image"]["height"] = 0;
                    break;
            }
        }
    }

    if ($arr["image"]["url"] != "") {
        $previewImages[] = $arr["image"]["url"];
    }

    $arr["maintext"] = bbCodeClear($arr["maintext"]);
    $arr["tags"] = json_decode($arr["tags"], true);
    $arr = stripRecursive($arr);
    $news[] = $arr;
}

$sm->assign("news", $news);

ob_start();
$sm->display("arc.html");
$text = ob_get_contents();
ob_end_clean();

$file = fopen("./archive.html", "w");
fputs($file, $text, mb_strlen($text));
fclose($file);

$arc = new PclZip("chief-online-news.zip");
$arc->create("archive.html," . implode(",", $previewImages));

header("Content-Type: application/zip");

$file = fopen("./chief-online-news.zip", "rb");
echo fread($file, filesize("./archive.html"));
fclose($file);
?>